﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.Configuration
{
    public partial class LeaveConfigure : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateLeave();
            }

        }

        protected void SaveLeave(object sender, EventArgs e)
        {
            try
            {
                Leave leave = new Leave();
                LeaveManager leaveManager = new LeaveManager();
                leave.EarnLeave = Convert.ToInt32(txtEarnLeave.Value);
                leave.SickLeave = Convert.ToInt32(txtSickLeave.Value);
                leave.CasualLeave = Convert.ToInt32(txtCasualLeave.Value);
                leave.Others = Convert.ToInt32(txtOthersleave.Value);
                if (leaveManager.CofigureLeave(leave))
                {
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Text = "Configure Leave Successfully";
                    PopulateLeave();

                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Text = "Sorry Can not Configure Leave!";

                }

            }
            catch (Exception ex)
            {

                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = ex.Message;
            }

        }

        public void PopulateLeave()
        {
            List<Leave> leaves = new List<Leave>();
            LeaveManager manager = new LeaveManager();
            Leave aLeave = new Leave();
            aLeave = manager.GetLeaveSettings();
            leaves.Add(aLeave);
            TotalLeaveGdv.DataSource = leaves;
            TotalLeaveGdv.DataBind();
        }

        protected void  ResetLeave(object o,EventArgs e)
        {
            try
            {
                int key = 0;
                if (resetLeaveRadio.Checked)
                {
                    key = 1;
                }
                if (resetAndAddPreviousPrevious.Checked)
                {
                    key = 2;
                }
                if (key > 0)
                {
                    LeaveManager manager = new LeaveManager();
                    if (manager.ResetLeave(key))
                    {
                        resetMsgLbl.ForeColor = Color.Green;
                        resetMsgLbl.Text = "Leave Reseting For All Employee";
                    }
                    else
                    {
                        resetMsgLbl.ForeColor = Color.Red;
                        resetMsgLbl.Text = "Leave Not Reset, Try again !";
                    }
                }
                else
                {
                    resetMsgLbl.ForeColor = Color.Red;
                    resetMsgLbl.Text = "Please Select a Reseting Mode !";
                }
            }
            catch (Exception ex)
            {
                resetMsgLbl.ForeColor = Color.Red;
                resetMsgLbl.Text = ex.Message;
            }
        }
    }
}